Ext.define('GBIF.ReclassifiyGrid', {
		extend: 'Ext.grid.Panel'	
	,	alias: 'widget.reclassifiyGrid'
	,	border: false
	,	autoScroll: true	
	,	viewConfig: {
				stripeRows: false
			,	emptyText: '<div style="padding:10px;">'+ this.txtEmptyText +'</div>'
    	,	style: { overflow: 'auto', overflowX: 'hidden' }
		}
	,	loadMask: true
	,	columnLines: true
	,	enableQuickMode: false
	,	enableColumnHide: false
	,	firstLoaded: false
  ,	scroll: false
	,	initComponent: function() {	
			this.store = Ext.create('Ext.data.Store', {
					pageSize: 25
				,	model: 'ClassifyModel'
				,	remoteSort: true
				,	proxy: {
							type: 'ajax'
						,	url: Config.baseUrl + 'resources/api/api.php'
						,	reader: {
									type: 'json'
								,	root: 'records'
								,	totalProperty: 'recordTotal'
							}
						,	simpleSortMode: true
					}
				,	sorters: [{
							property: 'scientificName_p'
						,	direction: 'ASC'
					}]
			});

			var fieldStore =  new Ext.create('Ext.data.SimpleStore', {
					fields: ['id','value']
				,	data: [
							['kingdom', 'Kingdom']
						,	['phylum', 'Phylum']
						,	['class', 'Class']
						,	['order', 'Order']
						,	['family', 'Family']
						,	['genus', 'Genus']
						,	['scientificName', 'Scientific Name']
					]
			});

			this.fieldCombo = Ext.create('Ext.form.field.ComboBox', {
					store: fieldStore
				,	width: 110	
				,	queryMode: 'local'
				,	displayField: 'value'
				,	valueField: 'id'
				,	typeAhead: false
				,	triggerAction: 'all'
				,	editable: false
				,	value: Config.fieldValue
			});

			this.store.on('beforeload', function(me, op, option){
					me.getProxy().extraParams['field'] = this.fieldCombo.getValue();
			}, this);
			
			this.tbar = [{
						width: 260
					,	fieldLabel: this.txtSearch
					,	labelWidth: 60
					,	xtype: 'searchfield'
					,	scope: this
					,	store: this.store
			}, this.fieldCombo, {
						text: this.txtStartClassification
					,	iconCls: 'icon_page_find'	
					,	scope: this
					,	handler: this.startClassifyWindow
				}
			]

			this.bbar = Ext.create('Ext.PagingToolbar', {
					store: this.store
				,	scope: this	
				,	displayInfo: true
				,	displayMsg: this.txtDisplayMsg
				,	emptyMsg: this.txtEmptyMsg
				,	updateInfo : function(){
						var me = this,
							displayItem = me.child('#displayItem'),
							store = me.store,
							pageData = me.getPageData(),
							count, msg;
		
						if (displayItem) {
							count = store.getCount();
							if (count === 0) {
								msg = me.emptyMsg;
							} else {
								msg = Ext.String.format(
									me.displayMsg,
									pageData.fromRecord,
									pageData.toRecord,
									Ext.util.Format.number(pageData.total, '0,000'),
									Ext.util.Format.number(store.proxy.reader.jsonData.totalRecordCount, '0,000')
								);
							}
							displayItem.setText(msg);
							me.doComponentLayout();
						}
					}
      });
			
			this.columns = [
					{header: "Record ID", width: 75, sortable: true, dataIndex: 'recordId', draggable: false}
				,	{header: "Scientific Name", width: 180, dataIndex:"scientificName_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'scientificName')}}
				,	{header: "Authorship", width: 180, dataIndex:"scientificNameAuthorship_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'scientificNameAuthorship')}}
				,	{header: "Genus", width: 95, dataIndex:"genus_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'genus')}}
				,	{header: "Family", width: 95, dataIndex:"family_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'family')}}
				,	{header: "Class", width: 80, dataIndex:"class_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'class')}}
				,	{header: "Order", width: 85, dataIndex:"order_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'order')}}
				,	{header: "Phylum", width: 95, dataIndex:"phylum_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'phylum')}}
				,	{header: "Kingdom", width: 65, dataIndex:"kingdom_p", sortable: true, draggable: false, renderer: function(value, p, r){return this.renderValue(p, r, 'kingdom')}}
				,	{header: "According To", width: 120, dataIndex:"taxonAccordingTo", sortable: true, draggable: false}
			]
			this.callParent();
		}
	,	startClassifyWindow: function() {
			if(this.getStore().getCount() != 0) {
				var pwindow = Ext.create('GBIF.ProcessWindow',{}).show();	
				pwindow.on('close', function(me, a, b, c){
					if (me.mode == 'process') {
						this.startClassify(me.pidForm.getValues(), me.primaryCombo.getRawValue(), me.secondaryCombo.getRawValue());
					}
				}, this);
			}
		}

	,	startClassify: function(data, primary, secondary){
			var storeCount = this.getStore().totalCount;
			var timeRequired = (parseInt(storeCount) * 0.7 / 60).toFixed(2);
			var str = Ext.util.Format.format(this.msgStartClssify, storeCount, timeRequired, primary, secondary);
			Ext.Msg.confirm(this.msgNoticeTitle, str, function(btn){
				if(btn == 'yes'){
					Ext.Ajax.request({
							url : Config.baseUrl + 'resources/api/api.php'
						,	params: {
									cmd: 'classify'
								,	pAuth: data.primary
								,	sAuth: data.secondary
								,	projectId: GBIF.currentProjectID
								,	quickMode: data.quickmode || 0
								,	highertaxa: data.highertaxa || 0
								, setAuthor: data.authorship
								, setTaxa: data.highertaxa || 0
							}
						,	scope: this
						,	success: function (response) {
								var o = Ext.decode(response.responseText);
								if(o.success){
										this.fireEvent('startClassification', o.status, this);
								} else {
										GBIF.Notice.msg(this.msgErrorTitle, o.error.errorMessage);
								}
							}
						,	failure: function () {
									GBIF.Notice.msg(this.msgErrorTitle, this.msgFatalError);
							}
					});
				}
			}, this);
		}

	,	renderValue: function(p, r, str){
			var orig = str + '_o';
			var newC = str + '_p';
			var orig1 = (r.data[orig] == 'NULL')? '&nbsp;':r.data[orig];
			var newC1 = (r.data[newC] == 'NULL')? '&nbsp;':r.data[newC];
			if(orig1 != newC1){
				if (GBIF.currentStatus != 'LOADED') {
					p.tdCls = 'tdStyle';
				}
				var str = Ext.util.Format.format('<div>{0}&nbsp;</div><div style="color:grey">{1}</div>', newC1, orig1);
				return str;
			}else{
				var str = Ext.util.Format.format('<div>{0}&nbsp;</div><div style="color:grey">{1}</div>', newC1, orig1);
				return str;
			}
				
		}
});